package com.liu.dw.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.dw.common.PageRequest;
import com.liu.dw.common.PageResult;
import com.liu.dw.common.PageUtils;
import com.liu.dw.entity.Perm;
import com.liu.dw.entity.Role;
import com.liu.dw.mapper.RolePermMapper;
import com.liu.dw.mapper.UserRoleMapper;
import com.liu.dw.mapper.UsersMapper;
import com.liu.dw.service.UsersService;
import com.liu.dw.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @version 1.0
 * @author： LiuWeiGang
 */

@SuppressWarnings("all")
@Service("UsersService")
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RolePermMapper rolePermMapper;


    /**
     * 登陆接口
     * @param user
     * @return
     */
    @Override
    public User findByUsername(User user) {
        return usersMapper.findByUsername(user.getUsername());
    }

    /**
     * 登陆接口
     * @param user
     * @return
     */
    @Override
    public User findUserById(String userId) {
        return usersMapper.findUserById(userId);
    }




    /**
     * @Description: 插入到用户和角色表
     */
    @Override
    public int saveObject(UserAddVo users, Integer[] roleIds) {
        //1.参数校验
        if(users==null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(users.getUsername()))
            throw new IllegalArgumentException("用户名不能为空");
        if(StringUtils.isEmpty(users.getPassword()))
            throw new IllegalArgumentException("密码不能为空");
        if(roleIds==null || roleIds.length==0)
            throw new IllegalArgumentException("至少要为用户分配角色");
        //新增的时候补全数据库中的字段
        users.setAdmin(false);
        //新增的时候补全数据库中的字段
        users.setSalt("lwg");
        int usersr = usersMapper.insertObject(users);
        //3.保存用户角色关系数据
        userRoleMapper.insertObjects(users.getId(), roleIds);
        //4.返回结果
        return usersr;
    }


    /**
     * 删除
     * @param data
     * @return
     */
    @Override
    public int deleteObjects(String[]  data) {
        //1.判定参数合法性
        if(data==null||data.length==0)
            throw new IllegalArgumentException("请选择一个");
        //2.执行删除操作
        int usersr;
        try{
            usersr = usersMapper.deleteObjects(data);
            userRoleMapper.deleteUsersById(data);
        }catch(Throwable e){
            e.printStackTrace();
            //发出报警信息(例如给运维人员发短信)
            throw new IllegalArgumentException("系统故障，正在恢复中...");
        }
        //4.对结果进行验证
        if(usersr==0)
            throw new IllegalArgumentException("记录可能已经不存在");
        //5.返回结果
        return usersr;
    }


    /**
     * 获取权限列表
     */
    @Override
    public List<Perm> getAllPerm() {
        List<Perm> perms = usersMapper.getAllPerm();
        return perms;
    }


    /**
     * 角色列表
     */
    @Override
    public PageResult findRoleVoPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageRoleInfo(pageRequest));
    }
    private PageInfo<RoleVo> getPageRoleInfo(PageRequest pageRequest) {
        int current = pageRequest.getCurrent();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(current, pageSize);
        List<RoleVo> roleVos = usersMapper.getAllRoleVo();
        return new PageInfo<RoleVo>(roleVos);
    }


    /**
     * 获取角色详情
     */
    @Override
    public List<RoleVo> getAllRoleVoById(Integer id) {
        List<RoleVo> roleVoById = usersMapper.getAllRoleVoById(id);
        return roleVoById;
    }

    /**
     * 创建角色
     */
    @Override
    public int saveRoleObject(Role role, Integer[] list) {
        //1.参数校验
        if(role==null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(role.getName()))
            throw new IllegalArgumentException("角色名不能为空");
        if(list==null || list.length==0)
            throw new IllegalArgumentException("至少要为角色分配权限");
        int roles = usersMapper.insertRoleObject(role);
        //3.保存用户角色关系数据
        rolePermMapper.insertRoleObjects(role.getId(),list);
        return roles;
    }


    /**
     * 删除角色
     * @param
     * @return
     */
    @Override
    public int deletePhotoByArray(String[]  data) {
        //1.判定参数合法性
        if(data==null||data.length==0)
            throw new IllegalArgumentException("请选择一个");
        //2.执行删除操作
        int rows;
        try{
            rows= usersMapper.deletePhotoByArray(data);
            rolePermMapper.deleteObjectsByRoleId(data);
        }catch(Throwable e){
            e.printStackTrace();
            //发出报警信息(例如给运维人员发短信)
            throw new IllegalArgumentException("系统故障，正在恢复中...");
        }
        //4.对结果进行验证
        if(rows==0)
            throw new IllegalArgumentException("记录可能已经不存在");
        //5.返回结果
        return rows;
    }

}